package testcase;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.testng.annotations.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class GetRequest {
    //Get带参
    public Document jsoup_get(String url,String param) throws IOException {
        Document doc=null;
       Connection conn= Jsoup.connect(url);
      conn.data("wd",param);

        doc=conn.get();
        return doc;
    }

    @Test
    public void testName() {

    }

    //get 请求带cookie
    public String getWithCookies(String url, String cookie) throws IOException {
        String result="";
        URL urll = new URL (url);
        HttpURLConnection conn = (HttpURLConnection )urll.openConnection ();
        conn.setRequestMethod ("GET");
        conn.setRequestProperty ("Cookie", cookie );
//－－－－－－－－－－－－或者这个？？？ conn.connect ();？？？
        conn.setDoInput (true );
        conn.setDoOutput (true );
       BufferedReader in= new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            result += line;
        }
        return result;
    }
    //post带参数
    public Document jsoup_post(String url,String param) throws IOException {
        Document doc=null;
        Connection conn= Jsoup.connect(url);
        System.out.println(conn.execute().cookies());



        //添加header信息
        conn.header("","");



        conn.data("charset","UTF-8");
        conn.data("tpl","mn");
        conn.data("codestring","");
        conn.data("index","0");
        conn.data("isPhone","false");
        conn.data("logloginType","pc_loginDialog");
        conn.data("men_pass","on");
        conn.data("ppui_logintime","14796");
        conn.data("safeflg","0");
        conn.data("staticpage","https://www.baidu.com/cache/user/html/v3Jump.html");
        conn.data("token","aad467d1b0392f297bdd1f90a5a1dcba");
        conn.data("splogin","rate");
        conn.data("traceid","FDAB0B01");
        conn.data("tt", "1534749519162");
        conn.data("u", "https://www.baidu.com/");
        //conn.data("token",getToken(cookie));
        doc=conn.post();
        return doc;
    }

    public static String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                    "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:52.0) Gecko/20100101 Firefox/52.0");
            conn.setRequestProperty("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
            conn.setRequestProperty("Upgrade-Insecure-Requests","1");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);

            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！" + e);
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }

        //测试方法
//    @Test
//    public void testName()throws IOException {
//        Document document = jsoup_get("https://www.baidu.com/s?ie=utf-8&newi=1&mod=1&isbd=1&isid=f5647dba0002c6e9&wd=loadrunner&rsv_spt=1&rsv_iqid=0xb244ef5e00013d6f&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&rqlang=cn&tn=baiduhome_pg&rsv_enter=0&oq=loadrunner&rsv_t=4ab0qvtT5vjAFiuzpnu2opyVHkyySkO%2FYZWAvGwlzzTYestCG0w2wuYcm9wypOAVFKdT&rsv_pq=f5647dba0002c6e9&inputT=169672&rsv_sug4=173652&bs=loadrunner&rsv_sid=1423_25809_26909_21108_18560_20880&_ss=1&clist=&hsug=&f4s=1&csor=10&_cr1=35412","性能测试");
//        System.out.println(document.toString());
//    }
//
//    @Test
//    public void testName1() throws IOException {
//        System.out.println( sendPost("https://passport.baidu.com/v2/api/?login","username=13714526025&password=maoyou"));
//
//    }

    @Test
    public void testName2() throws IOException {
        String response=getWithCookies("http://localhost:12306/mockPostwithcookie", "login=true");
        System.out.println(response.toString());
    }
}
